package com.enn.demo.application.assembler;

import com.enn.demo.client.command.PersonCommand;
import com.enn.demo.client.dto.PersonDTO;
import com.enn.demo.core.domain.model.person.Age;
import com.enn.demo.core.domain.model.person.Birthday;
import com.enn.demo.core.domain.model.person.Person;
import com.enn.demo.core.domain.model.person.PersonId;
import com.enn.demo.core.domain.model.user.UserName;

import java.util.Date;
import java.util.Optional;

/**
 * 用户Assembler
 *
 * @author haoxin
 * @date 2021-02-23
 **/
public class PersonDTOAssembler {

    public static Person toPerson(final PersonCommand personCommand) {
        return new Person(new UserName(personCommand.getUserName()),new Age(personCommand.getAge()),new Birthday(personCommand.getBirthday()));
    }

    public static PersonDTO fromPerson(final Person person) {
        PersonDTO personDTO = new PersonDTO();
        personDTO.setId(Optional.ofNullable(person.getPersonId()).orElse(new PersonId("0")).getId());
        personDTO.setUserName(Optional.ofNullable(person.getUserName()).orElse(new UserName("")).getName());
        personDTO.setAge(Optional.ofNullable(person.getAge()).orElse(new Age(0)).getAge());
        personDTO.setBirthday(Optional.ofNullable(person.getBirthday()).orElse(new Birthday(new Date())).getBirthday());
        personDTO.setStatus(person.getStatus() == null?null:person.getStatus().getValue());
        return personDTO;
    }
}
